<template>
  <d2-container>
    <div class="from_body">
      <div class="a">
        <span><h3>添加考核项</h3></span>
      </div>

      <!-- from -->
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="考核项">
          <el-input v-model="form.name" class="b"></el-input>
        </el-form-item>
        <el-form-item label="所属指标">
          <el-select v-model="form.ifpaName"  placeholder="请选择所属指标" class="b" @change="findByTheassessmentName">
            <el-option v-for="(item,index) in data" :key="index" :label="item.name" :value="item.name" >{{item.name}} </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考核种类">
          <el-select v-model="form.type" placeholder="请选择考核种类" class="b" @change="find">
            <el-option  v-for="(item,index) in dataType" :key="index" :label="item.assessmentTypes" :value="item.id">{{item.assessmentTypes}}</el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="权重">
          <el-input v-model="dataTypeStandard.weight" class="b">
      <!-- <el-input type="dataTypeStandard" v-model.number="numberValidateForm.age" autocomplete="off"></el-input> -->
    </el-input>
        </el-form-item>
         <el-form-item label="指标值">
          <el-input v-model="dataTypeStandard.targetedValue" class="b"></el-input>
        </el-form-item>
            <el-form-item label="考核办法">
          <el-input v-model="dataTypeStandard.assessStandard" class="b"></el-input>
        </el-form-item>
            <el-form-item label="归属" >
          <el-select v-model="dataTypeStandard.deptString" placeholder="请选择归属" class="b">
            <el-option label="手动录入" value="手动录入"></el-option>
            <el-option label="自动采集" value="自动采集"></el-option>
            <el-option label="定时生成" value="定时生成"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考核周期">
          <el-select v-model="dataTypeStandard.period" placeholder="请选择考核周期" class="b">
            <el-option label="月度" value="月度"></el-option>
            <el-option label="季度" value="季度"></el-option>
            <el-option label="年度" value="年度"></el-option>
          </el-select>
        </el-form-item>
         <!-- <el-form-item label="考核办法">
          <el-input v-model="form.assessmentMethod" class="b"></el-input>
        </el-form-item> -->
   <el-form-item label="权重类型">
          <el-select v-model="dataTypeStandard.weightType" placeholder="请选择考核周期" class="b">
            <el-option label="数值" value="数值"></el-option>
            <el-option label="否决指标" value="否决指标"></el-option>
            <el-option label="加扣项" value="加扣项"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="单位">
          <el-select v-model="dataTypeStandard.unit" placeholder="请选择单位" class="b">
            <el-option label="无" value="无"></el-option>
            <el-option label="分" value="分"></el-option>
            <el-option label="次" value="次"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计量单位">
          <el-input v-model="dataTypeStandard.unitOfmeasurement" class="b"></el-input>
        </el-form-item>

        <el-form-item label="数据来源">
          <el-select v-model="dataTypeStandard.datasources" placeholder="请选择数据来源" class="b">
            <el-option label="手动录入" value="手动录入"></el-option>
            <el-option label="自动采集" value="自动采集"></el-option>
            <el-option label="定时生成" value="定时生成"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </d2-container>
</template>

<style>
.b{
 width:60%;
}
.a {
  margin-left: 10%;
}
.from_body {
  width: 50%;
  margin: 0 auto;
  background-color: #ffffff;
}
.demo-ruleForm {
  margin: 0 auto;
}
</style>
<script>
import util from '@/libs/util'
export default {
  data () {
    return {
      form: {
        name: '',
        dept: '',
        datasources: '',
        unitOfmeasurement: '',
        unit: '',
        period: '',
        weight: '',
        weightType: '',
        type: '',
        ifpaName: '',
        typeId: '',
        assessmentMethod: ''
      },
      data: [
      ],
      dataType: [
      ],
      dataTypeStandard: {}
    }
  },
  created () {
    this.getTableList()
  },
  methods: {
    // 查询所有指标
    getTableList () {
      this.$request({
        url: 'Standard/findAll',
        method: 'post'
      }).then((res) => {
        console.log(res)
        this.data = res
      })
    },
    // 查询考核指标详情
    find () {
      console.log(this.form.type)
      this.$request({
        url: 'Standard/find',
        method: 'post',
        params: {
          name: this.form.ifpaName,
          assessmentTypes: this.form.type
        }
      }).then((res) => {
        console.log('res :>> ', res)
        this.dataTypeStandard = { ...res }
        // this.dataTypeStandard.weightType = res.weightType
        // this.dataTypeStandard.weight = res.weight
        // this.dataTypeStandard.targetedValue = res.targetedValue
        console.log('this.dataTypeStandard :>> ', this.dataTypeStandard)
      })
    },
    // 根据指标查询所有考核项
    findByTheassessmentName () {
      console.log(this.form.ifpaName)
      this.$request({
        url: 'type/findById',
        method: 'post',
        data: {
          name: this.form.ifpaName
        }
      }).then((res) => {
        this.dataType = res
      })
    },
    onSubmit () {
      const params = {
        name: this.form.name,
        ifpaName: this.form.ifpaName,
        type: this.form.type,
        weight: this.dataTypeStandard.weight,
        targetedValue: this.dataTypeStandard.targetedValue,
        assessStandard: this.dataTypeStandard.assessStandard,
        deptString: this.dataTypeStandard.deptString,
        unitOfmeasurement: this.dataTypeStandard.unitOfmeasurement,
        period: this.dataTypeStandard.period,
        weightType: this.dataTypeStandard.weightType,
        unit: this.dataTypeStandard.unit,
        datasources: this.dataTypeStandard.datasources,
        creatorString: util.cookies.get('uuid')
      }
      this.dialogFormVisibleRedact = true
      this.$request({
        url: 'item/addAssessItem',
        method: 'post',
        data: params
      }).then((res) => {
        this.$message.success('新增成功')
        this.$router.replace('assessItem')
      })
    }
  }
}
</script>
